package com.guigu.qingcheng.service_user.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.user.TbUser;
import com.guigu.qingcheng.pojo.user.UserOrder;
import com.guigu.qingcheng.service_user.service.TbUserService;
import com.guigu.qingcheng.service_user.service.UserOrderService;
import com.guigu.qingcheng.utils.MD5;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/service_user/tb-user")
public class TbUserController {

    @Autowired
    private TbUserService userService;

    @Autowired
    private UserOrderService userOrderService;

    @PostMapping("/user/findPage")
    public R findPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size, @RequestBody(required = false) Map<String,Object> searchMap){
        Page<TbUser> page1=userService.findPage(searchMap, page, size);
        long total=page1.getTotal();
        List<TbUser> list=page1.getRecords();
        if(list.size()>0){
            return R.ok().data("total",total).data("rows",list);
        }
        return R.ok().data("list",page1);
    }

    /**
     * 根据ID查询数据
     * @param username
     * @return
     */
    @GetMapping("/user/findById")
    public R findById(@RequestParam("username") String username){
        TbUser user=userService.getById(username);
        if(user==null){
            return R.error().data("user",user);
        }
        return R.ok().data("user",user);
    }

    /**
     * 增加数据
     * @param user
     * @return
     */
    @PostMapping("/user/add")
    public R add(@RequestBody TbUser user){
        boolean save = userService.save(user);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 修改数据
     * @param user
     * @return
     */
    @PostMapping("/user/update")
    public R update(@RequestBody TbUser user){
        boolean flag = userService.updateById(user);
        if(flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除数据
     * @param username
     * @return
     */
    @GetMapping("/user/delete")
    public R delete(@RequestParam("username") String username){
        boolean flag=userService.removeById(username);
        if(flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @PostMapping("save")
    public R save(@RequestBody TbUser user , String smsCode ){
        //密码加密
        String newpassword = MD5.encrypt(user.getPassword());
        user.setPassword(newpassword);
        return  userService.add(user,smsCode);
    }

    /**
     * 发送短信验证码
     * @param phone
     */
    @GetMapping(value="sendSms")
    public R sendSms(String phone){
        userService.sendSms(phone);
        return R.ok();
    }

    @PostMapping("login")
    public R loginUser (@RequestBody TbUser tbUser) {
        String login = userService.login(tbUser);
        return R.ok().data("login",login);
    }

    // 根据token获取用户信息
    @GetMapping("getUserInfo")
    public R getUserInfo(@RequestParam String username) {
        // 查询数据库根据用户id获取用户信息
        TbUser user = userService.getById(username);
        return R.ok().data("user",user);
    }

    @PostMapping("/user/selectUserOrder")
    public R selectUserOrder(@RequestBody(required = false) Map<String,Object> searchMap){
        List<UserOrder> list=userOrderService.selectUserOrder1(searchMap);
        return R.ok().data("list",list);
    }

}

